﻿@model CloudStorageLight.Core.Web.FilesVM
@{
    ViewBag.Title = "Cloud Storage Light " + Model.Current;
    Layout = "~/Views/Shared/_LayoutWebPart.cshtml";
    var parentUrl = Url.RouteUrl("webparts", new { id = CloudStorageLight.Core.BlobItem.GetDirectoryPath(Model.Current) }, this.Request.Url.Scheme) + Request.Url.Query;

    var folderUrl = Url.RouteUrl("files", new { id = CloudStorageLight.Core.BlobItem.GetDirectoryPath(Model.Current) }, this.Request.Url.Scheme) + Request.Url.Query;

    int pageSize = 5;
    if (Request.QueryString.AllKeys.Contains("pageSize"))
    {
        var pagesizeString = Request.QueryString["pageSize"];
        int.TryParse(pagesizeString, out pageSize);
    }
    bool showHeader = true;
    if (Request.QueryString.AllKeys.Contains("showHeader"))
    {
        var showHeaderString = Request.QueryString["showHeader"];
        bool.TryParse(showHeaderString, out showHeader);
    }
}

@section header{
    @if (!Model.User.IsGuest)
    {

    }
}



<div id="create-folder-dialog" style="display:none;">
    <input type="text" id="newFolderName" />
    <p>
        <div>
            <button id="create-newFolder" class="button" type="button">OK</button>
            <button class="button" type="button" onclick="$('#create-folder-dialog').dialog('close');">Cancel</button>
        </div>
    </p>
    @Html.Hidden("currentPath", Model.Current)
    @Html.Hidden("currentUser", Model.User.Name)
</div>

<div id="rename-dialog" style="display:none;">
    <input type="hidden" id="renameSrcPath" />
    <table style="width:100%;">
        <tr>
            <td width="50%">
                <div id="rename-directory-list"></div>
            </td>
            <td width="50%">
                <input type="text" id="renameDistName" style="width:100%;" />
            </td>
        </tr>
    </table>
    <p>
        <div>
            <button id="rename-command" class="button" type="button">OK</button>
            <button class="button" type="button" onclick="$('#rename-dialog').dialog('close');">Cancel</button>
        </div>
    </p>
    @Html.Hidden("currentPath", Model.Current)
</div>

<div id="move-dialog" style="display:none;">
    <table style="width:100%;">
        <tr>
            <td>
                <div id="move-directory-list">
                </div>
            </td>
        </tr>
    </table>
    <div>
        <button id="move-exec-command" class="button" type="button">OK</button>
        <button class="button" type="button" onclick="$('#move-dialog').dialog('close');">Cancel</button>
    </div>
    @Html.Hidden("currentPath", Model.Current)
</div>

<div id="download-confirm-dialog" style="display:none;">
    <p>@(CloudStorageLight.Core.Web.ViewMessages.MsgConfirmDownloadZip)</p>
    <div>
        <button id="download-zip-command" class="button" type="button">YES</button>
        <button id="download-files-command" class="button" type="button">NO</button>
    </div>
</div>

<form id="upload" enctype="multipart/form-data">
    @if (!this.Request.Browser.IsMobileDevice)
    {
        <input type="file" id="upl" name="upl" style="display:none;" multiple />
    }
    else
    {
        <input type="file" id="upl" name="upl" style="display:none;" />
    }
    @Html.Hidden("current", (Model.Current))
    @Html.Hidden("uploadId", Guid.NewGuid().ToString("d"))
</form>

<div id="folder-cont" style="display:none;">
    <div id="contextMenu-dialog" style="display:none;" class="bg-white">
        <ul class="vertical-menu compact" style="position: relative;z-index:1; margin:10px;">
            @if (Model.DownloadFromOriginalUrl)
            {
                <li><a class="original-download-context-command" href="javascript:void(0);">@(CloudStorageLight.Core.Web.ViewMessages.CmdDownload)</a></li>
            }
            else
            {
                <li><a class="download-context-command" href="javascript:void(0);">@(CloudStorageLight.Core.Web.ViewMessages.CmdDownload)</a></li>
            }
            @if (!this.Request.Browser.IsMobileDevice)
            {
                <li><a class="showurl-context-command" href="javascript:void(0);">@(CloudStorageLight.Core.Web.ViewMessages.CmdShowUrlLink)</a></li>
            }
            @if (!Model.User.IsGuest)
            {
                <li><a class="checkout-context-command" href="javascript:void(0);">@(CloudStorageLight.Core.Web.ViewMessages.CmdCheckOut)</a></li>
                <li><a class="checkin-context-command" href="javascript:void(0);">@(CloudStorageLight.Core.Web.ViewMessages.CmdCheckIn)</a></li>
                <li><a class="remove-context-command" href="javascript:void(0);">@(CloudStorageLight.Core.Web.ViewMessages.CmdRemove)</a></li>
                if (Model.BackupMode)
                {
                    if (Model.FolderControl)
                    {
                        <li><a class="compremove-context-command" href="javascript:void(0);">@(CloudStorageLight.Core.Web.ViewMessages.CmdDeleteCompletely)</a></li>
                    }
                }
            }
        </ul>
    </div>

    @if (Model.NotifyMessage != null)
    {
        <div class="notify-message">
            @(Model.NotifyMessage)
        </div>
    }

    @if (showHeader)
    {
        <div id="appTitle" class="place-left">
            <h3>
                @if (Model.Current.Length < 2)
                {
                    <span>
                        <span>
                            <a class="ms-heroCommandLink processing-link" href="@(Url.RouteUrl("webparts", new { id = "" }) + "/" + Request.Url.Query)">Home</a>
                        </span>
                    </span>
                }
                else
                {
                    string path = Url.RouteUrl("webparts", new { id = "" }) + "/";
                    string current = Model.Current.Substring(1, Model.Current.Length - 2);

                    if (current.Split('/').Length < 2)
                    {
                        <span>
                            <span>
                                <a class="ms-heroCommandLink processing-link" href="@(Url.RouteUrl("webparts", new { id = "" }) + "/" + Request.Url.Query)">Home</a>
                            </span>
                        </span>
                    }
                    var idx = 0;
                    foreach (var item in current.Split('/'))
                    {
                        idx++;
                        path += Uri.EscapeDataString(item) + "/";
                        if (idx <= current.Split('/').Length - 2)
                        {
                            continue;

                        }

                        <span>
                            <span>
                                <a class="ms-heroCommandLink processing-link" href="@(path + Request.Url.Query)">
                                    @(item)
                                    @if (Model.Folder != null && Model.Folder.IsPublic() && path.EndsWith(Model.Current))
                                    {
                                        <i class="icon-cloud"></i>
                                    }
                                </a>
                            </span>
                        </span>
                    }
                }
            </h3>
        </div>


        <div class="place-right">
            @if (!Model.User.IsGuest)
            {
                <div class="input-control text size3">
                    <input type="text" id="query" name="query" placeholder="Search..." value="">
                    <button id="search" class="btn-search"></button>
                </div>
            }
        </div>
        <div class="clearfix"></div>
        <div class="search-result-message">
        </div>
    }

    <div style="width:98%;">
        <table class="table" id="file-items" style="table-layout:fixed;">
            <thead>
                <tr>
                    <th class="file-items-header"></th>
                    <th class="file-items-name">@(CloudStorageLight.Core.Web.ViewMessages.MsgName)</th>
                    <th class="file-items-menu"></th>
                    <th class="file-items-date">@(CloudStorageLight.Core.Web.ViewMessages.MsgUpdatedAt)</th>
                    <th class="file-items-user">@(CloudStorageLight.Core.Web.ViewMessages.MsgUpdatedBy)</th>
                </tr>
            </thead>
            <tbody>
                @if (!showHeader && Model.Current != "/")
                {
                    <tr>
                        <td class="file-items-header"><a href="@(parentUrl)" class="blobfile"><i class="icon-folder-2 icon"></i></a></td>
                        <td class="file-items-name"><a href="@(parentUrl)" class="blobfile">..</a></td>
                        <td class="file-items-menu"></td>
                        <td class="file-items-date"></td>
                        <td class="file-items-user"></td>
                    </tr>
                }
            </tbody>
        </table>
        <div id="info-message"></div>
    </div>
</div>


<div id="directorySettings" style="height:100%;width:100%;display:none;">
</div>



<div id="file-drop-cont" style="display:none;">
    <span>
        @(CloudStorageLight.Core.Web.ViewMessages.MsgPleaseDropHere)
    </span>
</div>

<p id="page-top" style="display:none;"><a href="javascript:void(0);"><i class="icon-arrow-up-5"></i></a></p>

@section scripts{

    <script id="fileitem-template" type="text/x-jquery-tmpl">
        <tr role="row" class="ms-itmHoverEnabled ms-itmhover list" data-type="${Type}">
            {{if Type == "Directory"}}
            <td role="gridcell" class="ms-cellstyle file-items-header"><i class="${$data.Icon()} icon"></i></td>
            <td role="gridcell" class="ms-cellstyle file-items-name">
                <div class="ellipsis">
                    <a href="${$data.WebPartUrl()}" class="blobfile processing-link" data-href="${$data.WebPartUrl()}" data-path="${Path}">
                        <span class="item-name">${Title}</span>
                    </a>
                </div>
            </td>
            <td role="gridcell" class="ms-cellstyle file-items-menu">
                <div>
                </div>
            </td>
            <td role="gridcell" class="ms-cellstyle file-items-date"></td>
            <td role="gridcell" class="ms-cellstyle file-items-user">
                <div class="owner-userid">

                </div>
            </td>
            {{else}}
            <td role="gridcell" class="ms-cellstyle file-items-header">
                <i class="${$data.Icon()} icon"></i>
                <input type="checkbox" class="item-checkbox" name="${Path}" style="display:none;"/>
            </td>
            <td role="gridcell" class="ms-cellstyle file-items-name">
                <div>
                    <div class="ellipsis">
                        @if (Model.DownloadFromOriginalUrl)
                        {
                            <a href="${OriginalUrl}" target="original" class="blobfile" data-href="${OriginalUrl}" title="${Title}"><span class="item-name">${Title}</span></a>
                        }
                        else
                        {
                            <a href="${$data.AttachmentUrl()}" class="blobfile" data-href="${$data.AttachmentUrl()}" title="${Title}"><span class="item-name">${Title}</span></a>
                        }
                        <span class="item-marker">
                            {{if CheckoutUser }}
                            <i class="icon-pop-out fg-darkRed checkout-user" title="${CheckoutUser}"></i>
                            {{/if}}
                        </span>
                        <span style="padding-left:5px">(${$data.SizeString() })</span>                    </div>
                </div>
                <div>
                    <span class="list-remark fg-darkRed">
                    </span>
                </div>
            </td>
            <td role="gridcell" class="ms-cellstyle file-items-menu">
                <div class="conetextMenuBtn">
                    ...
                </div>
            </td>
            <td role="gridcell" class="ms-cellstyle file-items-date">
                <div class="last-modified ellipsis" title="${$data.LastModifiedString()}">
                    ${$data.LastModifiedString()}
                </div>
            </td>
            <td role="gridcell" class="ms-cellstyle file-items-user">
                <div class="owner-userid ellipsis" title="${Owner}">
                    ${Owner}
                </div>
            </td>
            {{/if}}
        </tr>
        {{if SearchContent }}
        <tr class="list">
            <td class="search-result ms-cellstyle"></td>
            <td colspan="4" class="search-result ms-cellstyle item-description">
                ${SearchContent}
            </td>
        </tr>
        {{/if}}
    </script>


    <script id="snapshot-template" type="text/x-jquery-tmpl">
        <tr>
            <td role="gridcell" class="ms-cellstyle file-items-header">
                <i class="icon-history icon"></i>
            </td>
            <td role="gridcell" class="ms-cellstyle file-items-name">
                <div>
                    <a href="${$data.Url()}" class="blobfile" data-href="${$data.Url()}">
                        <span class="item-name">${$data.Name()}</span>
                        <div>${$data.snapshotString()}</div>
                    </a>
                </div>
            </td>
            <td role="gridcell" class="ms-cellstyle file-items-menu">
                <div>
                </div>
            </td>
            <td role="gridcell" class="ms-cellstyle file-items-date">
            </td>
            <td role="gridcell" class="ms-cellstyle file-items-user">
            </td>
        </tr>
    </script>


    <script id="deleted-template" type="text/x-jquery-tmpl">
        <tr role="row" class="ms-itmHoverEnabled ms-itmhover list">
            <td role="rowheader" class="ms-cellstyle file-items-header">
                <input type="checkbox" class="item-checkbox" name="${path}" />
            </td>
            <td role="gridcell" class="ms-cellstyle">
                <i class="${$data.Icon()} icon"></i>
            </td>
            <td role="gridcell" class="ms-cellstyle">
                <div>
                    <span class="item-name">${$data.Name()}</span>
                </div>
                <div>
                    <span class="list-remark fg-darkRed">
                    </span>
                </div>
            </td>
            <td role="gridcell" class="ms-cellstyle"></td>
            <td role="gridcell" class="ms-cellstyle"></td>
            <td role="gridcell" class="ms-cellstyle"></td>
        </tr>
    </script>

    <script id="accesslog-template" type="text/x-jquery-tmpl">
        <tr>
            <td>
                ${$data.AccessDateString()}
            </td>
            <td>
                ${$data.Name()}
            </td>
            <td>
                ${ActionName}
            </td>
            <td>
                ${UserName}
            </td>
        </tr>
    </script>

    <script id="fileitem-more-template" type="text/x-jquery-tmpl">
        <tr class="list">
            <td colspan="5">
                <a href="@(folderUrl)" target="_blank">
                    @(CloudStorageLight.Core.Web.ViewMessages.MsgGoToWebSiteMore)
                </a>
            </td>
        </tr>
    </script>

    <script id="accesstoken-template" type="text/x-jquery-tmpl">
        <tr class="token-line">
            <td>
                {{if name }}
                <span>${name}</span>
                <input class="accessToken-name" type="hidden" value="${name}" />
                {{else}}
                <input class="accessToken-name" type="text" />
                {{/if}}
            </td>
            <td>
                <input type="checkbox" class="accessToken-can-read" />
            </td>
            <td>
                <input type="checkbox" class="accessToken-can-write" />
            </td>
            <td>
                <input type="checkbox" class="accessToken-can-modify" />
            </td>
            <td>
                <input type="checkbox" class="accessToken-can-control" />
            </td>
        </tr>

    </script>

    <script type="text/javascript">

        /*WebParts Resize*/
        $(document).ready(function () {
            getParameterByName = function (name) {
                var match = RegExp('[?&]' + name + '=([^&]*)')
                .exec(window.location.search);
                return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
            };
            try {
                var target = parent.postMessage ? parent : (parent.document.postMessage ? parent.document : undefined);
                var height = jQuery('#folder-cont').outerHeight(true);
                var width = "100%";

                target.postMessage('<message senderId=' + getParameterByName("SenderId") + '>resize(' + width + ',' + height + ')</message>', '*');
            } catch (e) {
                alert(e);
            }
        });


        $(function () {

            var dragHelp = false;

            $(document).bind("dragenter", function (evt) {
                var oev = evt.originalEvent;
                if (!oev.dataTransfer.types || oev.dataTransfer.types.length == 0 || oev.dataTransfer.types[0] != "Files") return;
                evt.stopPropagation();
                evt.preventDefault();
                if (dragHelp) return;
                dragHelp = true;
                $('#file-drop-cont').show();
            });

            $("#file-drop-cont").bind("dragleave", function (evt) {
                evt.stopPropagation();
                evt.preventDefault();
                if (!dragHelp) return;
                dragHelp = false;
                $('#file-drop-cont').hide();
            });

            $("#file-drop-cont").click(function () {
                $('#file-drop-cont').hide();
            });

            $('#upload').fileupload({
                dropZone: $('#file-drop-cont'),
                maxChunkSize: 50000000,
                singleFileUploads: true,
                add: function (e, data) {
                    CloudStorageLight.StorageController.upload(data);
                    $('#file-drop-cont').hide();
                },
                progressall: function (e, data) {
                    var progress = parseInt(data.loaded / data.total * 100, 10);
                    if (progress < 100)
                    {
                        $('#freezePanel .prog-percent').text(progress + '%');
                    } else {
                        $('#freezePanel .prog-percent').text('');
                    }
                },
                done: function (e, data) {
                }
            });

            var topBtn = $('#page-top');
            topBtn.hide();
            $(window).scroll(function () {
                if ($(this).scrollTop() > $(window).height()) {
                    topBtn.fadeIn();
                } else {
                    topBtn.fadeOut();
                }
            });
            topBtn.click(function () {
                $('body,html').animate({
                    scrollTop: 0
                }, 500);
                return false;
            });

            $(window).bottom({ proximity: 0.05 });

            $("#search").click(function(){
                $(".paging-nav").remove();
            });

            $("#query").keypress(function (e) {
                if (e.keyCode == 13) {
                    $(".paging-nav").remove();
                }
            });

            $("body").on("click", "a.processing-link", function (event) {
                event.preventDefault();
                var dataUrl = $(this).attr("href");
                if (dataUrl != "") {
                    CloudStorageLight.StorageController.blockUI(null, false);
                    window.location.href = dataUrl;
                }

            });
       
            CloudStorageLight.StorageController.getFiles($("#file-items"), '@(Model.Current)', 0, 200, function () {
                $('#file-items').paging({ limit: @(pageSize)});
                $("#folder-cont").show();
            });
        });

        function renderChromeOption() {
        }

        //CloudStorageLight.LyncUtil.init();

    </script>
}
